﻿using System.IO;
using NUnit.Framework;
using RadaCode.SwissKnife.Search;

namespace SwissKnife.Tests
{
    [TestFixture]
    public class BingImageRetrieverTests
    {
        private static string _filePath = @"C:\Data\images.cache";

        string apiKey = "348AA76657F3789E28FFEB896234EFB3111ADC4C"; //Go to http://www.bing.com/toolbox/bingdeveloper/ to register one if you don't have it yet

        [Test]
        public void GetRandomImageTest()
        {
            var retriever = new BingImageRetriever(apiKey, ImageFormat.Binary);

            var randomImageString = retriever.GetRandomImage();

            Assert.IsNotEmpty(randomImageString);
        }

        [Test]
        public void GetNImagesTest()
        {
            var requiredNumberOfImages = 5;

            var retriever = new BingImageRetriever(apiKey, ImageFormat.Binary);

            var images = retriever.GetRandomImages(requiredNumberOfImages);

            Assert.AreEqual(requiredNumberOfImages, images.Length);
        }

        [Test]
        public void GetNImagesWithCachingTest()
        {
            var requiredNumberOfImages = 5;
            var preCacheString = "Cat";

            using (var fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var xmlCacher = new XmlImageCacher(fileStream);

                var retriever = new BingImageRetriever(apiKey, ImageFormat.Binary, xmlCacher, preCacheString);

                var images = retriever.GetRandomImages(requiredNumberOfImages);

                Assert.AreEqual(requiredNumberOfImages, images.Length);
            }
        }

        [Test]
        public void GetOneRandomImagesWithCachingTest()
        {
            var requiredNumberOfImages = 5;

            using (var fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var xmlCacher = new XmlImageCacher(fileStream);

                var retriever = new BingImageRetriever(apiKey, ImageFormat.Binary, xmlCacher);

                var image = retriever.GetRandomImage();

                Assert.IsNotEmpty(image);
            }
        }

        [Test]
        public void GetNRandomImagesWithCachingTest()
        {
            var requiredNumberOfImages = 3;

            using (var fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var xmlCacher = new XmlImageCacher(fileStream);

                var retriever = new BingImageRetriever(apiKey, ImageFormat.Binary, xmlCacher);

                var image = retriever.GetRandomImages(requiredNumberOfImages);

                Assert.IsNotEmpty(image);
                Assert.AreEqual(requiredNumberOfImages, image.Length);
            }
        }

        [Test]
        public void GetNExactImagesWithCachingTest()
        {
            var requiredNumberOfImages = 3;
            var preCacheString = "Cat";

            using (var fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var xmlCacher = new XmlImageCacher(fileStream);

                var retriever = new BingImageRetriever(apiKey, ImageFormat.Binary, xmlCacher, preCacheString);

                var image = retriever.GetImagesByKeyword(preCacheString, requiredNumberOfImages);

                Assert.IsNotEmpty(image);
                Assert.AreEqual(requiredNumberOfImages, image.Length);
            }
        }
    }
}
